<html>
<body>
	<%@ page language="java" import="java.sql.*"%>
	<%
		try {
			Class.forName("org.postgresql.Driver");
			Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/cse132b","cse132blogin", "1234567890");
			String action = request.getParameter("action");
			// Check if an insertion is requested
			if (action != null && action.equals("insert")) {
				PreparedStatement pstmt;
				

				
				pstmt = conn.prepareStatement("INSERT INTO enroll (student_id,section_id,units) VALUES (?,?,?)");
				String prereqs[] = request
						.getParameterValues("prereqs");
				
				//out.print(request.getParameterValues("prereqs"));
				
				for (int i = 0; i < prereqs.length; i++) {
					PreparedStatement statement3 = conn
							.prepareStatement("INSERT INTO prerequisite_of(high_course_id, low_course_id) VALUES (?,?)");
					statement3.setInt(2,
							Integer.parseInt(prereqs[i]));
					statement3.setInt(1, Integer.parseInt(request.getParameter("sectionid")));
					statement3.executeUpdate();
					statement3.close();
				}
				
				int rowCount = pstmt.executeUpdate();
				out.println("Successfully enrolled student.<br/>");
			}

			
			if ( request.getParameter("sectionid") == null ) {
				Statement classes = conn.createStatement();
				ResultSet rsClasses = classes.executeQuery("select * from courses");
	%>
	
<table border="1">
		<tr>
			<td valign="top" width="40%">
				<%-- -------- Include menu HTML code -------- --%> <jsp:include
					page="/menu.html" />
			</td>
			<td>	
	<table border="0">
		<tr>
			<th>Select a class to add prereqs to</th>
		</tr>
		<tr>
			<th>Section ID</th>
			<th>Course Name</th>
		</tr>
		<%
		while (rsClasses.next()) {
		%>
		
		<tr>
			<td align="center">
				<a href="prereqs.jsp?sectionid=<%=rsClasses.getInt("course_id") %>"><%=rsClasses.getInt("course_id") %></a>
			</td>
			<td>
				<a href="prereqs.jsp?sectionid=<%=rsClasses.getInt("course_id") %>"><%=rsClasses.getString("name") %></a>
			</td>
			
		</tr>
		<%
		}
		%>
	</table>
	</td>
</tr>
</table>
	<% 
			} else {
				Statement courses = conn.createStatement();
				ResultSet rsCourses = courses.executeQuery("SELECT * FROM courses");
	%>
	<table border="1">
		<tr>
			<td valign="top" width="40%">
				<%-- -------- Include menu HTML code -------- --%> <jsp:include
					page="/menu.html" />
			</td>
			<td>
	<table border="0">
		<tr>
			<td valign="top">Enrollment Page</td>
		</tr>
		<tr>
			<table border="1">
				<tr>
					<th>Student*</th>
					<th>Units*</th>
				</tr>
				<tr>
					<form action="prereqs.jsp" method="get">
						<input type="hidden" value="insert" name="action">
						<input type="hidden" value="<%=request.getParameter("sectionid") %>" name="sectionid">
						<td>
							<select multiple name="prereqs">
								<%
								while(rsCourses.next()) {
								%>
									<option value="<%=rsCourses.getInt("course_id") %>"><%=rsCourses.getString("name") %></option>
								<%
								}
								%>
							</select>
						</td>
						<td><input type="submit"></input></td>
					</form>
				</tr>
			</table>
		</tr>
	</table>
	<%
			}
		} catch (SQLException ex) {
			out.print("SQLException: " + ex.getMessage() + "<br/>");
			out.print("SQLState: " + ex.getSQLState() + "<br/>");
			out.print("VendorError: " + ex.getErrorCode() + "<br/>");
		}
	%>

</td>
</tr>
</table>
</body>
</html>